package tasks.zhuanyeji;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TaskFenYeQi {


    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        int maxCount = scanner.nextInt();
//        int pageCount = scanner.nextInt();
//        int currentPage = scanner.nextInt();
//        scanner.close();

        int maxCount = 100;
        int pageCount = 10;
        int currentPage = 5;

        //情况1 maxCount <=pageCount
        if (maxCount <= pageCount) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 1; i <= maxCount; i++) {
                if (i > 1) {
                    stringBuilder.append(" ");
                }
                stringBuilder.append(i);
            }
            System.out.println(stringBuilder.toString());
            return;
        }
        // 情况2 总页数大于页码数
        List<Object> tokens = new ArrayList<>();

        boolean showFirst = true;
        boolean showLast = true;
        boolean showCurrent = true;

        if (currentPage == 1 || currentPage == maxCount) {
            showCurrent = false;
        }

        // 计算显示的固定的元素数量
        int fixedCount = 0;

        if (showFirst) {
            fixedCount++;
        }

        if (showLast && maxCount != 1) {
            fixedCount++;
        }
        if (showCurrent && currentPage != 1 && currentPage != maxCount) {
            fixedCount++;
        }
        // 接下来计算需要几个 逗号
        int minDots = 0;
        if (maxCount > 2) {
            if (currentPage > 2) { // 1 和 currentPage可能需要逗号
                minDots++;
            }

            if (currentPage < maxCount - 1) { // current Page和maxCount可能需要逗号
                minDots++;
            }

            minDots = Math.max(1, minDots);
        }
        // 计算可用的槽位
        int availableSlots = pageCount - fixedCount - minDots;
        availableSlots = Math.max(0, availableSlots);

        // 计算前后可分配的页码范围
        int frontAvailable = (currentPage > 2) ? (currentPage - 2) : 0; // 2到currentPage-1
        int backAvailable = (currentPage < maxCount - 1) ? (maxCount - currentPage - 1) : 0;// currentPage到maxCount-1

        // 分配额外页码 x=前面，y=后面
        int x, y;
        if (currentPage == 1) {
            x = 0;// 前面不能分配
            y = Math.min(availableSlots, backAvailable);
        } else if (currentPage == maxCount) {
            x = Math.min(availableSlots, frontAvailable);
            y = 0;
        } else {
            // 一般场景 1<currentPage<maxCount
            x = Math.min(frontAvailable, availableSlots / 2);
            y = Math.min(backAvailable, availableSlots - x);

            int bestDiff = Math.abs(x - y);
            int bestX = x;

            for (int testX = x; testX <= Math.min(frontAvailable, availableSlots); testX++) {
                int testY = Math.min(backAvailable, availableSlots - testX);
                int diff = Math.abs(testY - testX);
                if (diff < bestDiff || (diff == bestDiff && testX > bestX)) {
                    bestDiff = diff;
                    bestX = testX;
                }
            }
            x = bestX;
            y = Math.min(backAvailable, availableSlots - x);
        }

        tokens.add(1);

        if (currentPage > 2) {
            boolean needFrontDot = currentPage - x > 2;
            if (needFrontDot) {
                tokens.add("...");
            }
            int startPage = currentPage - x;
            for (int i = startPage; i < currentPage; i++) {
                if (i > 1) {
                    tokens.add(i);
                }
            }

        } else if (currentPage == 2 && maxCount > 2) {
            tokens.add(2);
        }
        if (currentPage != 1 && currentPage != maxCount) {
            tokens.add(currentPage);
        }

        // 处理当前页后面的部分
        if (currentPage < maxCount - 1) {
            int endPage = currentPage + y;
            for (int i = currentPage + 1; i <= endPage; i++) {
                if (i < maxCount) {
                    tokens.add(i);
                }
            }

            boolean needBackDot = endPage < maxCount - 1;
            if (needBackDot) {
                tokens.add("...");
            }
        } else if (currentPage == maxCount - 1 && maxCount > 2) {
            tokens.add(maxCount - 1);
        }

        if (maxCount != 1) {
            tokens.add(maxCount);
        }

        // 确保元素的数量等于pageCount
        while (tokens.size() > pageCount) {
            boolean removed = false;

            for (int i = tokens.size() - 2; i >= 0; i--) {
                if (tokens.get(i) instanceof Integer &&
                        (Integer) tokens.get(i) > currentPage &&
                        (Integer) tokens.get(i) < maxCount) {
                    tokens.remove(i);
                    removed = true;
                    break;
                }
            }

            // 其次移除当前页前面的页码
            if (!removed) {
                for (int i = 1; i < tokens.size(); i++) {
                    if (tokens.get(i) instanceof Integer &&
                            (Integer) tokens.get(i) < currentPage &&
                            (Integer) tokens.get(i) > 1) {
                        tokens.remove(i);
                        removed = true;
                        break;
                    }
                }
            }

            // 最后考虑移除省略号（不应该发生，但安全起见）
            if (!removed && tokens.size() > 3) {
                for (int i = 1; i < tokens.size() - 1; i++) {
                    if (tokens.get(i).equals("...")) {
                        tokens.remove(i);
                        break;
                    }
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tokens.size(); i++) {
            if (i > 0) sb.append(" ");
            sb.append(tokens.get(i));
        }
        System.out.println(sb.toString());

    }

}
